package com.ruoyi.web.controller.course;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.DateUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.course.domain.SysStudentPaper;
import com.ruoyi.course.service.ISysStudentPaperService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学员试卷Controller
 * 
 * @author YJF
 */
@RestController
@RequestMapping("/student/paper")
public class SysStudentPaperController extends BaseController
{
    @Autowired
    private ISysStudentPaperService sysStudentPaperService;

    /**
     * 查询学员试卷列表
     */
    @PreAuthorize("@ss.hasPermi('student:paper:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysStudentPaper sysStudentPaper)
    {
        startPage();
        List<SysStudentPaper> list = sysStudentPaperService.selectSysStudentPaperList(sysStudentPaper);
        for(SysStudentPaper l :list){
            //时长
            if (l.getBeginTime() != null && l.getFinishTime() != null) {
                String diffTime = DateUtils.getTaskDatePoor(l.getFinishTime(),l.getBeginTime());
                l.setElapsedTimeStr(diffTime);
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出学员试卷列表
     */
    @PreAuthorize("@ss.hasPermi('student:paper:export')")
    @Log(title = "学员试卷", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysStudentPaper sysStudentPaper)
    {
        List<SysStudentPaper> list = sysStudentPaperService.selectSysStudentPaperList(sysStudentPaper);
        ExcelUtil<SysStudentPaper> util = new ExcelUtil<SysStudentPaper>(SysStudentPaper.class);
        util.exportExcel(response, list, "学员试卷数据");
    }

    /**
     * 获取学员试卷详细信息
     */
    @PreAuthorize("@ss.hasPermi('student:paper:query')")
    @GetMapping(value = "/{studentPaperId}")
    public AjaxResult getInfo(@PathVariable("studentPaperId") Long studentPaperId)
    {
        return AjaxResult.success(sysStudentPaperService.selectSysStudentPaperByStudentPaperId(studentPaperId));
    }

    /**
     * 新增学员试卷
     */
    @PreAuthorize("@ss.hasPermi('student:paper:add')")
    @Log(title = "学员试卷", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysStudentPaper sysStudentPaper)
    {
        return toAjax(sysStudentPaperService.insertSysStudentPaper(sysStudentPaper));
    }

    /**
     * 修改学员试卷
     */
    @PreAuthorize("@ss.hasPermi('student:paper:edit')")
    @Log(title = "学员试卷", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysStudentPaper sysStudentPaper)
    {
        return toAjax(sysStudentPaperService.updateSysStudentPaper(sysStudentPaper));
    }

    /**
     * 删除学员试卷
     */
    @PreAuthorize("@ss.hasPermi('student:paper:remove')")
    @Log(title = "学员试卷", businessType = BusinessType.DELETE)
	@DeleteMapping("/{studentPaperIds}")
    public AjaxResult remove(@PathVariable Long[] studentPaperIds)
    {
        return toAjax(sysStudentPaperService.deleteSysStudentPaperByStudentPaperIds(studentPaperIds));
    }
}
